//
//  ForgetThePasswordViewController.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/20.
//

import UIKit

class ForgetThePasswordViewController: BaseViewController {
    private var isPhone = true
    private var totalTime: Int = 60
    private var timer: Timer?
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavi()
        
        setupUI()
    }
    private func setupNavi() {
        navigationItem.title = "忘记密码"
    }
    private func setupUI() {
        [phoneButton, emailButton, blueLineView, serviceLabel].forEach { subView in
            view.addSubview(subView)
        }
        phoneButton.snp.makeConstraints { make in
            make.top.equalTo(48)
            make.left.equalTo(0)
            make.height.equalTo(25)
        }
        emailButton.snp.makeConstraints { make in
            make.left.equalTo(phoneButton.snp.right)
            make.centerY.equalTo(phoneButton.snp.centerY)
            make.height.equalTo(phoneButton.snp.height)
            make.width.equalTo(phoneButton.snp.width)
            make.right.equalToSuperview()
        }
        blueLineView.snp.makeConstraints { make in
            make.centerX.equalTo(phoneButton.snp.centerX)
            make.top.equalTo(phoneButton.snp.bottom).offset(5)
            make.width.equalTo(60)
            make.height.equalTo(2)
        }
        serviceLabel.snp.makeConstraints { make in
            make.bottom.equalTo(-ScreenSize.safeAreaInsets.bottom)
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
        }
        setPhoneVerificationAction()
    }
    private func setPhoneVerificationAction() {
        timer?.invalidate()
        timer = nil
        [countryView, phoneTextField, codeTextField, passwordTextField, confirmButton].forEach { subView in
            subView.removeFromSuperview()
        }
        view.addSubviews(countryView, phoneTextField, codeTextField, passwordTextField, confirmButton)
        countryView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(blueLineView.snp.bottom).offset(13)
            make.height.equalTo(60)
        }
        phoneTextField.attributedPlaceholder = NSAttributedString(string: "请输入手机号", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        phoneTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(countryView.snp.bottom).offset(20)
            make.height.equalTo(60)
        }
        codeTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(phoneTextField.snp.bottom).offset(20)
            make.height.equalTo(60)
        }
        passwordTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(codeTextField.snp.bottom).offset(20)
            make.height.equalTo(60)
        }
        confirmButton.snp.makeConstraints { make in
            make.top.equalTo(passwordTextField.snp.bottom).offset(28)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(50)
        }
    }
    private func setEmailVerificationAction() {
        timer?.invalidate()
        timer = nil
        [countryView, phoneTextField, codeTextField, passwordTextField, confirmButton].forEach { subView in
            subView.removeFromSuperview()
        }
        view.addSubviews(phoneTextField, codeTextField, passwordTextField, confirmButton)
        
        phoneTextField.attributedPlaceholder = NSAttributedString(string: "请输入邮箱", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        
        phoneTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(blueLineView.snp.bottom).offset(13)
            make.height.equalTo(60)
        }
        codeTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(phoneTextField.snp.bottom).offset(20)
            make.height.equalTo(60)
        }
        passwordTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(codeTextField.snp.bottom).offset(20)
            make.height.equalTo(60)
        }
        confirmButton.snp.makeConstraints { make in
            make.top.equalTo(passwordTextField.snp.bottom).offset(28)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(50)
        }
    }
    //=================================================================
    //                           事件处理
    //=================================================================
    // MARK: - 事件处理
    @objc private func phoneButtonAction() {
        isPhone = true
        
        phoneButton.isSelected = true
        emailButton.isSelected = false
        blueLineView.snp.remakeConstraints { make in
            make.centerX.equalTo(phoneButton.snp.centerX)
            make.top.equalTo(phoneButton.snp.bottom).offset(5)
            make.width.equalTo(60)
            make.height.equalTo(2)
        }
        setPhoneVerificationAction()
    }
    @objc private func emailButtonAction() {
        isPhone = false
        
        phoneButton.isSelected = false
        emailButton.isSelected = true
        blueLineView.snp.remakeConstraints { make in
            make.centerX.equalTo(emailButton.snp.centerX)
            make.top.equalTo(emailButton.snp.bottom).offset(5)
            make.width.equalTo(60)
            make.height.equalTo(2)
        }
        setEmailVerificationAction()
    }
    @objc private func getNumButtonAction () {
        
        getNumButton.isUserInteractionEnabled = false
        timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { [weak self] timer in
            if let number = self?.totalTime, number > 0 {
                self?.getNumButton.setTitle("\(number)s", for: .normal)
                self?.getNumButton.setTitle("\(number)s", for: .selected)
                self?.totalTime -= 1
            }else {
                self?.getNumButton.setTitle("获取验证码", for: .normal)
                self?.getNumButton.setTitle("获取验证码", for: .selected)
                self?.timer?.invalidate()
                self?.timer = nil
                self?.getNumButton.isUserInteractionEnabled = true
            }
        })
    }
    @objc private func confirmButtonAction() {
        print("确认")
        let isPhoneEmpty = phoneTextField.text?.isEmpty ?? false
        let isCode = codeTextField.text?.isEmpty ?? false
        let isPassword = passwordTextField.text?.isEmpty ?? false
        
        if isPhoneEmpty {
            Toast.with(message: isPhone ? "请输入手机号" : "请输入邮箱").showOnWindow()
            return
        }
        if isCode {
            Toast.with(message: "请输入验证码").showOnWindow()
            return
        }
        if isPassword {
            Toast.with(message: "请输入密码").showOnWindow()
            return
        }
        Toast.with(message: "修改密码成功").showOnWindow()
        navigationController?.popViewController(animated: true)
    }
    @objc private func serviceLabelTapAction() {
        print("联系客服")
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var phoneButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("手机验证", for: .normal)
        button.setTitle("手机验证", for: .selected)
        button.setTitleColor(.lxs.hex("#808080"), for: .normal)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(phoneButtonAction), for: .touchUpInside)
        button.isSelected = true
        return button
    }()
    private lazy var emailButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("邮箱验证", for: .normal)
        button.setTitle("邮箱验证", for: .selected)
        button.setTitleColor(.lxs.hex("#808080"), for: .normal)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(emailButtonAction), for: .touchUpInside)
        button.isSelected = false
        return button
    }()
    private lazy var blueLineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = .lxs.hex("#57A0FF")
        return lineView
    }()
    private lazy var countryView: CustomCountryNumView = {
        let country = CustomCountryNumView()
        return country
    }()
    private lazy var phoneTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入手机号", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var codeTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入验证码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = passwordTextFieldRightView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var passwordTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "填写新密码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var getNumButton: UIButton = {
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 20, y: 0, width: 70, height: 60)
        button.setTitle("获取验证码", for: .normal)
        button.setTitle("获取验证码", for: .selected)
        button.setTitleColor(.lxs.hex("#57A0FF"), for: .normal)
        button.setTitleColor(.lxs.hex("#57A0FF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 14)
        button.addTarget(self, action: #selector(getNumButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var passwordTextFieldRightView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 111, height: 60))
        view.addSubview(getNumButton)
        return view
    }()
    private lazy var confirmButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setTitle("确认", for: .normal)
        button.setTitle("确认", for: .selected)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .normal)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(confirmButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var serviceLabel: UILabel = {
        let label = UILabel()
        let prefixStr = "没有绑定账号? "
        let suffixStr = "联系客服"
        let attr = NSMutableAttributedString(string: "\(prefixStr)\(suffixStr)")
        attr.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)], range: NSRange(location: 0, length: prefixStr.count))
        attr.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.lxs.hex("#FFFFFF"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)], range: NSRange(location: prefixStr.count, length: suffixStr.count))
        label.attributedText = attr
        label.textAlignment = .center
        let tap = UITapGestureRecognizer(target: self, action: #selector(serviceLabelTapAction))
        label.addGestureRecognizer(tap)
        return label
    }()
}
